/**
* 
* Copyright [2007] [Sunny Liu]
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software
*   distributed under the License is distributed on an "AS IS" BASIS,
*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*   See the License for the specific language governing permissions and
*   limitations under the License.
*/
package ${appConfig.outputPackageName}.model;

<#if entity.hasDateField >
import java.util.Date;
</#if>
<#if entity.hasRelationships && !appConfig.webServiceApp >
import ${appConfig.outputPackageName}.service.${appConfig.appJavaName}Service;
</#if>


public class ${entity.javaName}
{
	<#list entity.attributes as attribute>
	<#if attribute.dateType><#if attribute.hasFormatString>
	private static final java.text.SimpleDateFormat ${attribute.javaName}Sdf = new java.text.SimpleDateFormat("${attribute.formatString}");	
	<#else>
	private static final java.text.SimpleDateFormat ${attribute.javaName}Sdf = new java.text.SimpleDateFormat();	
	</#if></#if>	
	</#list>	
	<#if entity.hasRelationships && !appConfig.webServiceApp>
	private ${appConfig.appJavaName}Service appService;
	
	<#list entity.relationships as relationship>
    <#if relationship.hasMultipleTarget >
    private java.util.List ${relationship.targetEntity.normalizedJavaName}List;	
    <#else>
    private	${relationship.targetEntity.javaName} ${relationship.targetEntity.normalizedJavaName};
    </#if>
    </#list>	
	</#if>
	
	<#list entity.attributes as attribute>
	<#if attribute.usedInJava >
		private ${attribute.javaType} ${attribute.javaName};	
	</#if>		
	</#list>
	
	<#list entity.attributes as attribute>
		<#if attribute.usedInJava >
		public void set${attribute.setterGetterName}(${attribute.javaType} ${attribute.javaName})
		{
			<#if !attribute.hasSetter >
			this.${attribute.javaName} = ${attribute.javaName};
			<#else>			
			${attribute.setter}
			</#if>			
		}
				
		public ${attribute.javaType} <#if attribute.booleanType >is<#else>get</#if>${attribute.setterGetterName}()
		{
			<#if !attribute.hasGetter >
			return this.${attribute.javaName};
			<#else>			
			${attribute.getter}
			</#if>			
		}
		<#if attribute.dateType>
		public void set${attribute.setterGetterName}String(String ${attribute.javaName}String)
		{
			
			try{
				this.${attribute.javaName} = ${attribute.javaName}Sdf.parse(${attribute.javaName}String);
			}catch(Exception ex){
				;
			}								
		}
				
		public String get${attribute.setterGetterName}String()
		{			
			if(this.${attribute.javaName}==null) return "";
			return ${attribute.javaName}Sdf.format(this.${attribute.javaName});					
		}		
		</#if>			
		</#if>
	</#list>
	
	
	<#if entity.hasRelationships && !appConfig.webServiceApp>	
	public void set${appConfig.appJavaName}Service(${appConfig.appJavaName}Service service)
	{
		this.appService = service;	
	}	
	<#list entity.relationships as relationship>
	<#if relationship.hasMultipleTarget >
	public java.util.List get${relationship.targetEntity.javaName}List()
	{				
		//this only uses lazy loading;
		if(this.appService!=null){
		   return this.appService.find${relationship.targetEntity.javaName}By${entity.javaName}(this);
		}else{
		   return  this.${relationship.targetEntity.normalizedJavaName}List;
		}		
	}
	
	public void set${relationship.targetEntity.javaName}List(java.util.List ${relationship.targetEntity.normalizedJavaName}List)
	{
		this.${relationship.targetEntity.normalizedJavaName}List = ${relationship.targetEntity.normalizedJavaName}List;
	}    
	<#else>
	public ${relationship.targetEntity.javaName} get${relationship.targetEntity.javaName}()
	{    		
		//this only uses lazy loading;
		if(this.appService!=null){
		   return this.appService.find${relationship.targetEntity.javaName}By${entity.javaName}(this);
		}else{
		   return  this.${relationship.targetEntity.normalizedJavaName};
		}	
	}
	
	public void set${relationship.targetEntity.javaName}(${relationship.targetEntity.javaName} ${relationship.targetEntity.normalizedJavaName})
	{
		this.${relationship.targetEntity.normalizedJavaName} = ${relationship.targetEntity.normalizedJavaName};
	}
	</#if>	
    </#list>	
	</#if>
	
	<#if entity.hasMethods >
	${entity.methods}			
	</#if>	
	
	public int hashCode()
	{
		return toString().hashCode();
	}
	
	public boolean equals(Object obj)
	{
		if(obj!=null && obj instanceof ${entity.javaName})
		{
			${entity.javaName} another = (${entity.javaName}) obj;
			return (another.hashCode() == hashCode());
		
		}		
		return false;
	}
	
	public String toString()
	{
		StringBuffer buffer = new StringBuffer();
		<#if entity.hasPrimaryKey >
		<#list entity.primaryKeyColumns as attribute>
		buffer.append(String.valueOf(${attribute.javaName}));
		<#if attribute_has_next> 
		buffer.append("|");
		</#if>
		</#list>
		<#else>
		<#list entity.attributes as attribute>
		buffer.append(String.valueOf(${attribute.javaName}));
		<#if attribute_has_next> 
		buffer.append("|");
		</#if>
		</#list>
		</#if>
		return buffer.toString();
	}

}